import * as categoryDao from '../dao/g_category.dao';

export async function getCategoryList(query: any, offset: number = 0, limit: number = 10) {
    return await categoryDao.getCategoryList(offset, limit);
}

export async function getCategoryById(id: number) {
    return await categoryDao.getCategoryById(id);
}

export async function createCategory(data: any) {
    if (data.fid===undefined) {
        data.fid = 0
    }
    let descs = data.descs;
    delete data.descs;
    let id = await categoryDao.createCategory(data);
    if (id >0) {
        if (descs) {
        for (let desc of descs) {
            desc.alias = data.alias;
                await categoryDao.editCategoryDesc(desc);
            }
        }
    }
    return id;
}

export async function updateCategory(id: number, data: any) {
    let descs = data.descs;
    delete data.descs;
    if (descs) {
        for (let desc of descs) {
            desc.alias = data.alias;
            await categoryDao.editCategoryDesc(desc);
        }
    }
    return await categoryDao.updateCategory(id, data);
}

export async function deleteCategory(id: number) {
    return await categoryDao.deleteCategory(id);
} 